package com.txr.news.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.txr.news.customexception.BusinessException;
import com.txr.news.customexception.SystemException;
import com.txr.news.mapper.ArticleMapper;
import com.txr.news.pojo.ExType;
import com.txr.news.pojo.ResultArticle;
import com.txr.news.pojo.entity.Article;
import com.txr.news.pojo.entity.ArticleCate;
import com.txr.news.pojo.entity.ArticleExample;
import com.txr.news.service.ArticleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class ArticleServiceImpl implements ArticleService {
    ArticleMapper articleMapper;

    @Override
    public Boolean addArticle(Article article) {
        int flag = articleMapper.insertSelective(article);
        if (flag != 1){
            return false;
        }
        return true;
    }

    /**
     * 获取文章列表：
     * 1、从数据库查询所有未删除文章数据
     * 2、
     * @return
     */
    /*@Override
    public List<Article> getArticleList() {
        List<Article> articleList = articleMapper.getArticleAndCateByStepOne(false);
        //List<Article> articleList = articleMapper.getArticleAndCate(false);
        System.out.println(articleList);
        if (articleList == null || articleList.isEmpty()){
            throw new BusinessException("article not found", ExType.BussRecordNotFound);
        }
        //List<ArticleCate> articleCates=  articleList.stream().map(Article::getArticleCate).collect(Collectors.toList());
        return articleList;
    }*/

    /**
     * 删除文章：
     * 1、根据 id 查询数据库谁否有该文章 如果没有 返回false
     * 2、如果有 进行删除操作：把is_delete修改为1
     * @param id
     * @return
     */
    @Override
    public Boolean deleteArticle(String id) {
        Integer newid = Integer.parseInt(id);
        Article article = articleMapper.selectByPrimaryKey(newid);
        if (article == null){
            return false;
        }
        article.setIsDelete(true);
        int flag = articleMapper.updateByPrimaryKeySelective(article);
        if (flag != 1){
            throw new SystemException("delete failed", ExType.SystemInsertFail);
        }
        return true;
    }

    /**
     * 获取文章列表：
     * 1、进行分页操作
     * 2、从数据库查询所有未删除文章数据
     * 3、得到分页 总条数
     * 4、返回数据给 控制层
     * @return
     */
    @Override
    public ResultArticle getArticleList(int pagenum, int pagesize, String cate_id, String state) {
        ResultArticle result = new ResultArticle(0,"获取文章列表成功！",null,null,null);

        PageHelper.startPage(pagenum,pagesize);

        if (cate_id == null || cate_id.equals("")){
            List<Article> articleList = articleMapper.getArticleAndCate(false,null,state);
            if (articleList == null || articleList.isEmpty()){
                throw new BusinessException("article not found", ExType.BussRecordNotFound);
            }
            //拿到其他分页信息
            PageInfo<Article> info = new PageInfo<>(articleList);
            //得到 总共有多少条记录
            Integer total = Math.toIntExact(info.getTotal());
            result.setTotal(total);
            result.setData(articleList);
            return result;
        }

        List<Article> articleList = articleMapper.getArticleAndCate(false,Integer.parseInt(cate_id),state);
        if (articleList == null || articleList.isEmpty()){
            throw new BusinessException("article not found", ExType.BussRecordNotFound);
        }

        //拿到其他分页信息
        PageInfo<Article> info = new PageInfo<>(articleList);
        //得到 总共有多少条记录
        Integer total = Math.toIntExact(info.getTotal());
        result.setTotal(total);
        result.setData(articleList);
        return result;
    }

    /**
     * 根据id获取文章信息：
     * 1、根据id查询文章信息，判断查询结果是否为空
     * 2、返回查询出的对象给控制层
     * @param article_id
     * @return
     */
    @Override
    public Article getArticleById(Integer article_id) {
        Article article = articleMapper.selectByPrimaryKey(article_id);
        if (article == null){
            throw new SystemException("article is not found by" + article_id,ExType.SystemFileNotFound);
        }
        return article;
    }
}
